﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GameNesSL
{
    using System.Windows.Browser;
    using System.Windows.Threading;

    [ScriptableType]
    public partial class MainPage : UserControl
    {
        [ScriptableMember]
        public event EventHandler GameLoaded = null;

        private void InvokeGameLoaded()
        {
            EventHandler handler = GameLoaded;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        public MainPage()
        {
            HtmlPage.RegisterScriptableObject("Game", this);
            InitializeComponent();
            GameContext.GameLoaded+=(delegate { InvokeGameLoaded(); });
            //contentFrame.Navigate(new Uri("/StartPage", UriKind.Relative));
            if (HtmlPage.IsEnabled && HtmlPage.Document.QueryString != null)
            {
                if (HtmlPage.Document.QueryString.ContainsKey(WebService.WebServiceMethods.QueryGameID))
                {
                    GameContext.webMethods.GameID =
                        HtmlPage.Document.QueryString[WebService.WebServiceMethods.QueryGameID];
                }
                if (HtmlPage.Document.QueryString.ContainsKey(WebService.WebServiceMethods.QueryUserID))
                {
                    GameContext.webMethods.UserID =
                        HtmlPage.Document.QueryString[WebService.WebServiceMethods.QueryUserID];
                }
            }

            GameContext.stats.UserID = GameContext.webMethods.UserID;
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            
        }
        
        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (!e.Uri.IsAbsoluteUri)
            {
                e.Handled = true;
                GameContext.ErrorDescription = e.Exception.Message;
                contentFrame.Navigate(new Uri("/Error", UriKind.Relative));
            }
        }

        private void contentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (e.NavigationMode==NavigationMode.New && e.Uri.IsAbsoluteUri)
            {
                HtmlPage.Window.Navigate(e.Uri);
            }
        }

        public void ErrorMsg(Exception ex)
        {
            GameContext.stats.GotErrors++;
            error.Visibility = System.Windows.Visibility.Visible;
            errordetails.Text = ex.Message;
        }

        private void fontbutton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            error.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}